<?php
namespace app\index\service;


use app\index\model\AccountListModel;
use app\index\model\DealcomModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginModel;
use app\index\model\IncomeEnginModel;
use app\index\model\IncomeHuanModel;
use app\index\model\IncomeQiModel;
use app\index\model\IncomeYingModel;
use app\index\model\PiaokaiModel;

class IncomeService extends CommonService
{

    public function create_income_engin_number(){
        $tou = 'XMSK';
        $daystr = substr(date("Ymd"), 2);
        $number = IncomeEnginModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function create_income_qi_number(){
        $tou = 'QTSK';
        $daystr = substr(date("Ymd"), 2);
        $number = IncomeQiModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_bond_shou_number(){
        $tou = 'BZJSQ';
        $daystr = substr(date("Ymd"), 2);
        $number = EnginBondShouModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_income_huan_number(){
        $tou = 'JZHK';
        $daystr = substr(date("Ymd"), 2);
        $number = IncomeHuanModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function create_income_ying_number(){
        $tou = 'YSK';
        $daystr = substr(date("Ymd"), 2);
        $number = IncomeYingModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function get_income_shoutype(){
        return [
            1 => '项目收款',
            4 => '其他收款',
            7 => '保证金收取',
        ];
    }
    public function get_income_shoutype_name($shoutype){
        $arr = $this->get_income_shoutype();
        $str = isset($arr[$shoutype]) ? $arr[$shoutype] : '';
        return $str;
    }

    public function get_income_ying_daomoney($data){
        $money = 0;
        if($data['shoutype'] == 1){
            $money = IncomeEnginModel::where(['deleted_user'=>0,'mal'=>1,'yingid'=>$data['id']])->sum('money');
        }
        if($data['shoutype'] == 4){
            $money = IncomeQiModel::where(['deleted_user'=>0,'yingid'=>$data['id']])->sum('money');
        }
        if($data['shoutype'] == 7){
            $money = EnginBondShouModel::where(['deleted_user'=>0,'yingid'=>$data['id']])->sum('money');
        }
        return $money;
    }


    public function create_account_list_jiaonum_shou(){
        $tou = 'SR';
        $daystr = substr(date("Ymd"), 2);
        $number = AccountListModel::where('jiaonum','like','%'.$tou.$daystr.'%')->order('jiaonum DESC')->value('jiaonum');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function create_account_income_engin($mid, $data){
        $data['enginid'] = isset($data['enginid']) ? $data['enginid'] : 0;
        $data['enginname'] = EnginModel::where(['id'=>$data['enginid']])->value('name');
        if(!$data['enginname']){
            $data['enginname'] = '';
        }
        $ins = [];
        $ins['comid'] = $this->userinfo['comid'];
        $ins['type'] = 1;
        $ins['mid'] = $mid;
        $ins['enginid'] = $data['enginid'];
        $ins['enginname'] = $data['enginname'];
        $ins['jiaonum'] = $this->create_account_list_jiaonum_shou();
        $ins['money'] = $data['money'];
        $ins['budid'] = $data['budid'];
        $ins['zhai'] = $data['zhai'];
        $ins['faer'] = $data['faer'];
        $ins['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['incdaotype']);
        $ins['dt'] = $data['dt'];
        $ins['accid'] = $data['accid'];
        $ins['kaiid'] = isset($data['kaiid']) ? $data['kaiid'] : 0;
        $ins['baner'] = $data['baner'];
        $ins['created_user'] = $this->userinfo['id'];
        $ins['updated_user'] = $this->userinfo['id'];
        $ins['created_at'] = date("Y-m-d H:i:s");
        $ins['updated_at'] = date("Y-m-d H:i:s");


        AccountListModel::insert($ins);

        if($ins['kaiid'] > 0){
            $piaomoney = AccountListModel::where(['kaiid'=>$ins['kaiid']])->sum('money');
            PiaokaiModel::where(['id'=>$ins['kaiid']])->update(['accmoney'=>$piaomoney]);
        }

        return true;
    }
    public function update_account_income_engin($mid, $data){
        $data['enginid'] = isset($data['enginid']) ? $data['enginid'] : 0;
        $data['enginname'] = EnginModel::where(['id'=>$data['enginid']])->value('name');
        if(!$data['enginname']){
            $data['enginname'] = '';
        }
        $upd = [];
        $upd['money'] = $data['money'];
        $upd['budid'] = $data['budid'];
        $upd['zhai'] = $data['zhai'];
        $upd['faer'] = $data['faer'];
        $upd['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['incdaotype']);
        $upd['dt'] = $data['dt'];
        $upd['accid'] = $data['accid'];
        $upd['kaiid'] = $data['kaiid'];
        $upd['baner'] = $data['baner'];
        $upd['updated_user'] = $this->userinfo['id'];
        $upd['updated_at'] = date("Y-m-d H:i:s");


        $updInfo = AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>1,'mid'=>$mid])->find();
        if($updInfo){
            if($updInfo['kaiid'] > 0){
                $piaomoney = AccountListModel::where(['kaiid'=>$updInfo['kaiid']])->where('id','neq',$updInfo['id'])->sum('money');
                PiaokaiModel::where(['id'=>$updInfo['kaiid']])->update(['accmoney'=>$piaomoney]);
            }

        }
        AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>1,'mid'=>$mid])->update($upd);

        if($upd['kaiid'] > 0){
            $piaomoney = AccountListModel::where(['kaiid'=>$upd['kaiid']])->sum('money');
            PiaokaiModel::where(['id'=>$upd['kaiid']])->update(['accmoney'=>$piaomoney]);
        }

        return true;
    }
    public function del_account_income_engin($mid){
        AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>1,'mid'=>$mid])->delete();
        return true;
    }

    public function create_account_income_qi($mid, $data){
        $ins = [];
        $ins['comid'] = $this->userinfo['comid'];
        $ins['type'] = 2;
        $ins['mid'] = $mid;
        $ins['enginid'] = 0;
        $ins['enginname'] = '';
        $ins['jiaonum'] = $this->create_account_list_jiaonum_shou();
        $ins['money'] = $data['money'];
        $ins['budid'] = $data['budid'];
        $ins['zhai'] = $data['zhai'];
        $ins['faer'] = $data['faer'];
        $ins['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['incdaotype']);
        $ins['dt'] = $data['dt'];
        $ins['accid'] = $data['accid'];
        $ins['baner'] = $data['baner'];
        $ins['created_user'] = $this->userinfo['id'];
        $ins['updated_user'] = $this->userinfo['id'];
        $ins['created_at'] = date("Y-m-d H:i:s");
        $ins['updated_at'] = date("Y-m-d H:i:s");

        AccountListModel::insert($ins);
        return true;
    }
    public function update_account_income_qi($mid, $data){
        $upd = [];
        $upd['enginid'] = 0;
        $upd['enginname'] = '';
        $upd['money'] = $data['money'];
        $upd['budid'] = $data['budid'];
        $upd['zhai'] = $data['zhai'];
        $upd['faer'] = $data['faer'];
        $upd['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['incdaotype']);
        $upd['dt'] = $data['dt'];
        $upd['accid'] = $data['accid'];
        $upd['baner'] = $data['baner'];
        $upd['updated_user'] = $this->userinfo['id'];
        $upd['updated_at'] = date("Y-m-d H:i:s");
        AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>2,'mid'=>$mid])->update($upd);
        return true;
    }
    public function del_account_income_qi($mid){
        AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>2,'mid'=>$mid])->delete();
        return true;
    }




    public function create_bond_shou_engin($mid, $data){
        $ins = [];
        $ins['comid'] = $this->userinfo['comid'];
        $ins['type'] = 9;
        $ins['mid'] = $mid;
        $ins['enginid'] = $data['enginid'];
        $ins['bidid'] = $data['bidid'];
        $ins['jiaonum'] = $this->create_account_list_jiaonum_shou();
        $ins['money'] = $data['money'];
        $ins['budid'] = $data['budid'];
        $ins['zhai'] = $data['zhai'];
        $ins['faer'] = $data['gonger'];
        $ins['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['incdaotype']);
        $ins['dt'] = $data['dt'];
        $ins['accid'] = $data['accid'];
        $ins['baner'] = $data['baner'];
        $ins['created_user'] = $this->userinfo['id'];
        $ins['updated_user'] = $this->userinfo['id'];
        $ins['created_at'] = date("Y-m-d H:i:s");
        $ins['updated_at'] = date("Y-m-d H:i:s");

        AccountListModel::insert($ins);
        return true;
    }
    public function update_bond_shou_engin($mid, $data){
        $upd = [];
        $ins['mid'] = $mid;
        $ins['enginid'] = $data['enginid'];
        $ins['bidid'] = $data['bidid'];
        $upd['money'] = $data['money'];
        $upd['budid'] = $data['budid'];
        $upd['zhai'] = $data['zhai'];
        $upd['faer'] = $data['gonger'];
        $upd['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['incdaotype']);
        $upd['dt'] = $data['dt'];
        $upd['accid'] = $data['accid'];
        $upd['baner'] = $data['baner'];
        $upd['updated_user'] = $this->userinfo['id'];
        $upd['updated_at'] = date("Y-m-d H:i:s");
        AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>2,'mid'=>$mid])->update($upd);
        return true;
    }
    public function del_bond_shou_engin($mid){
        AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>9,'mid'=>$mid])->delete();
        return true;
    }




    public function get_incshou_paytype(){
        return [
            1 => '项目收入',
            2 => '其他收入',
        ];
    }

    public function get_incshou_paytype_name($paytype){
        $arr = $this->get_incshou_paytype();
        $str = isset($arr[$paytype]) ? $arr[$paytype] : '';
        return $str;
    }

}

